@(username: String, human: String, bot: String, money: String, wpisowe: String)

$(function() {
    var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket
 var chatSocket = new WS("@routes.Application.chat(username,human,bot,money,wpisowe).webSocketURL(request)")


	var sendMessage = function() 
	{
		chatSocket.send(JSON.stringify({nr: $("#nr").val()}))
        	$("#nr").val('')
	}
	var betMessage = function() 
	{
		chatSocket.send(JSON.stringify({bet: $("#betinput").val()}))
        	$("#betinput").val('')
	}
	var raiseMessage = function() 
	{
		chatSocket.send(JSON.stringify({raise: $("#raiseinput").val()}))
        	$("#raiseinput").val('')
	}
	var askForCards = function(ok) 
	{
		chatSocket.send(JSON.stringify({start:ok}))
    	}	
	var checkMessage = function(ok) 
	{
		chatSocket.send(JSON.stringify({check:ok}))
    	}
	var callMessage = function(ok) 
	{
		chatSocket.send(JSON.stringify({cal:ok}))
    	} 	
	var foldMessage = function(ok) 
	{
		chatSocket.send(JSON.stringify({fold:ok}))
    	}
	var allinMessage = function(ok) 
	{
		chatSocket.send(JSON.stringify({allin:ok}))
    	}

   	

	var receiveEvent = function(event) 
	{
		var data = JSON.parse(event.data)
		// Handle errors
		if(data.error) 
		{
		    chatSocket.close()
		    $("#onError span").text(data.error)
		    $("#onError").show()
		    return
		} 
		else 
		{
		    $("#onChat").show()
		}
		
		// Create the message element       
		var el = $('<div class="message"><p style="font-size:16px"></p></div>')
		$("p", el).text(data.message)
		$(el).addClass('me')
		$('#messages').append(el) 
	}

    	var handleReturnKey = function(e) 
	{
        	if(e.charCode == 13 || e.keyCode == 13) 
		{
            		e.preventDefault()
            		sendMessage()
        	}
    	}	

	$('input[name="start"]').click(function(){ askForCards("1") })
	$('input[name="send"]').click(function(){ sendMessage() })
	$('input[name="bet"]').click(function(){ betMessage() })
	$('input[name="raise"]').click(function(){ raiseMessage() })
	$('input[name="check"]').click(function(){ checkMessage("2") })
	$('input[name="call"]').click(function(){ callMessage("3") })
	$('input[name="fold"]').click(function(){ foldMessage("4") })
	$('input[name="allin"]').click(function(){ allinMessage("5") })

	chatSocket.onmessage = receiveEvent

})
